package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.lawEnforcing.dto.WritDataDto;
import com.jiantong.lawEnforcing.impl.dao.DatasDao;
import com.jiantong.lawEnforcing.impl.manager.DatasManager;
import com.jiantong.lawEnforcing.model.Datas;
import com.jiantong.lawEnforcing.service.DatasService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 存储表
 * Created by mz03 on 2017/8/14.
 */
@Slf4j
@Service
@RpcProvider
public class DatasServiceImpl implements DatasService {

    private final DatasDao datasDao;
    private final DatasManager datasManager;
    @Autowired
    public DatasServiceImpl(DatasDao datasDao, DatasManager datasManager) {
        this.datasDao = datasDao;
        this.datasManager = datasManager;
    }

    /**
     * id 查找
     *
     * @param id
     * @return News
     */
    @Override
    public Response<Datas> finById(Long id) {
        try {
            Datas datas = datasDao.findById(id);
            if (datas != null) {
                return Response.ok(datas);
            } else {
                log.error("datas (id={}) is not found", id);
                return Response.fail("datas.not.found");
            }
        }catch (Exception e){
            log.error("failed to find datas(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("datas.find.fail");
        }
    }

    /**
     * @param map
     * @return
     */
    @Override
    public Response<List<Datas>> findDataList(Map<String, Object> map) {
        try {
            List<Datas> list = datasDao.findDataList(map);
            if (list != null) {
                return Response.ok(list);
            } else {
                log.error("查询文书类型下所有已制作的文书失败");
                return Response.fail("查询文书类型下所有已制作的文书失败");
            }
        }catch (Exception e){
            log.error("查询文书类型下所有已制作的文书失败,cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("查询文书类型下所有已制作的文书失败");
        }
    }

    /**
     * 根据案件id和文书类型,查出目前是第几次创建此文书
     *
     * @param cases_id
     * @param origin_name
     * @return
     */
    @Override
    public Response<Integer> findDatasCount(Long cases_id, String origin_name) {
        try {
            Map<String,Object> map = Maps.newHashMap();
            if (cases_id != null) {
                map.put("law_case", cases_id);
            }
            if (origin_name != null) {
                map.put("origin_name",origin_name);
            }
            ;
            return Response.ok(datasDao.findDatasCount(map));
        }catch (Exception e) {
            log.error("failed.to.create.datas:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.datas.failed");
        }
    }

    /**
     * 根据一组originids 查找一组Datas
     *
     * @param map
     * @return
     */
    @Override
    public Response<List<Datas>> findByOriginIds(Map<String, Object> map) {
        try {
            WritDataDto writDataDto = new WritDataDto();

            List<Datas> datasList = datasDao.findByOriginIds(map);
            if (datasList != null) {
                return Response.ok(datasList);
            } else {
                log.error("datasList (ids={}) is not found", map);
                return Response.fail("datasList.not.found");
            }
        }catch (Exception e){
            log.error("failed to find datasLsit cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("datasList.find.fail");
        }
    }

    /**
     * 创建新单据
     *
     * @param datas 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(Datas datas) {
        try {
            datasDao.create(datas);
            return Response.ok(datas.getId());
        }catch (Exception e) {
            log.error("failed.to.create.datas:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.datas.failed");
        }
    }

    /**
     * 创建新闻新单据
     *
     * @param datasList 单据对象
     * @return 本次创建了多少条信息
     */
    @Override
    public Response<Integer> creates(List<Datas> datasList) {
        try {
            return Response.ok(datasDao.creates(datasList));
        } catch (Exception e) {
            log.error("failed.to.create.datas.list, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.datas.list.failed");
        }

    }

    /**
     * 更新单据
     *
     * @param datas 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(Datas datas) {
        try{
            return Response.ok(datasDao.update(datas));
        }catch (Exception e){
            log.error("failed.to.update.datas:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("update.datas.failed");
        }
    }


    /**
     * 删除单据
     *
     * @param writDataDto 单据对象
     * @return 删除的条数
     */
    @Override
    public Response<Integer> deletesByOriginIds(WritDataDto writDataDto) {
        try {
            return Response.ok(datasManager.deletesByOriginIds(writDataDto));
        } catch (Exception e) {
            log.error("failed.to.delete, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("delete.datas.failed");
        }
    }
}
